package Pet.Management.controller;

import Pet.Management.domain.entity.PetCareOrder;
import Pet.Management.repository.PetCareOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/pet-care/orders")
public class PetCareOrderListController {
    
    private static final Logger logger = LoggerFactory.getLogger(PetCareOrderListController.class);

    @Autowired
    private PetCareOrderRepository orderRepository;

    @GetMapping("/published/{userId}")
    public ResponseEntity<Map<String, Object>> getPublishedOrders(@PathVariable Long userId) {
        logger.info("获取用户{}发布的订单", userId);
        try {
            List<PetCareOrder> orders = orderRepository.findByPublisherId(userId);
            logger.info("找到{}条发布的订单", orders.size());
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "获取成功");
            response.put("data", orders);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取发布订单列表失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("msg", "获取订单列表失败: " + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/accepted/{userId}")
    public ResponseEntity<Map<String, Object>> getAcceptedOrders(@PathVariable Long userId) {
        logger.info("获取用户{}接受的订单", userId);
        try {
            List<PetCareOrder> orders = orderRepository.findByAcceptorId(userId);
            logger.info("找到{}条接受的订单", orders.size());
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "获取成功");
            response.put("data", orders);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取接受订单列表失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("msg", "获取订单列表失败: " + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
}